import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:moose_app/core/utils/navigate_util.dart';
import 'package:moose_app/ui/screens/qr_scan/qr_scan_screen.dart';
import 'package:moose_app/ui/values/colors.dart';
import 'package:moose_app/ui/values/constants.dart';
import 'package:moose_app/ui/widgets/scroll/scroller_configuration_wrapper.dart';

import 'food/food_content.dart';
import 'home_banner.dart';

class ATHHomeBody extends StatefulWidget {
  @override
  _ATHHomeBodyState createState() => _ATHHomeBodyState();
}

class _ATHHomeBodyState extends State<ATHHomeBody> {
  EasyRefreshController _easyRefreshController;

  @override
  void initState() {
    super.initState();
    _easyRefreshController = EasyRefreshController();
  }

  @override
  Widget build(BuildContext context) {
    return ATHScrollerConfigurationWrapper(
      child: EasyRefresh.custom(
        enableControlFinishRefresh: true,
        enableControlFinishLoad: true,
        controller: _easyRefreshController,
        header: MaterialHeader(),
        footer: MaterialFooter(),
        onRefresh: () async {
          await Future.delayed(Duration(seconds: 2), () {
            _easyRefreshController.finishRefresh(success: true);
          });
        },
        onLoad: () async {
          await Future.delayed(Duration(seconds: 2), () {});
        },
        slivers: <Widget>[
          SliverToBoxAdapter(
            child: Container(
              height: kSize200,
              decoration: BoxDecoration(
                  gradient:
                      LinearGradient(colors: [kColorPrimary, kColorPrimary])),
              child: Container(
                margin: EdgeInsets.symmetric(horizontal: kSize20),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    _buildOperatorItem(),
                    _buildOperatorItem(),
                    _buildOperatorItem(),
                    _buildOperatorItem(),
                  ],
                ),
              ),
            ),
          ),
          SliverToBoxAdapter(child: _buildGridView()),
          SliverToBoxAdapter(
            child: ATHHomeBanner(),
          ),
          SliverToBoxAdapter(
            child: ATHFoodContent(),
          ),
        ],
      ),
    );
  }

  Widget _buildGridView() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: kSize20, vertical: kSize20),
      child: GridView.builder(
        shrinkWrap: true,
        itemCount: 10,
        physics: NeverScrollableScrollPhysics(),
        padding: EdgeInsets.zero,
        itemBuilder: (context, index) {
          return Column(
            children: [
              ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(kSize16)),
                  child: CachedNetworkImage(
                    imageUrl:
                        "https://gitee.com/shizidada/moose-resource/raw/master/avatar/avatar-girl.jpeg",
                    fit: BoxFit.fill,
                  )),
              Container(
                margin: EdgeInsets.only(top: kSize8),
                child: Text(
                  "美食",
                  style: TextStyle(
                      fontSize: kFontSize30, fontWeight: FontWeight.w500),
                ),
              ),
            ],
          );
        },
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 5,
          mainAxisSpacing: kSize16,
          crossAxisSpacing: kSize16,
          childAspectRatio: 1 / 1.5,
        ),
      ),
    );
  }

  Widget _buildOperatorItem() {
    return GestureDetector(
      onTap: () {
        ATHNavigator.pushFromRight(context, ATHQrScanScreen.routeName);
      },
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          SvgPicture.asset(
            "assets/icons/icon_scan.svg",
            width: kSize80,
          ),
          Container(
            margin: EdgeInsets.only(top: kSize10),
            child: Text(
              "扫码",
              style: TextStyle(fontSize: kFontSize32),
            ),
          ),
        ],
      ),
    );
  }
}
